package com.example.gulimall.order.config;

import com.example.gulimall.order.entity.OrderEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 *延迟队列流程：
 * 1生产者生产消息到交换机orderEventExchange，并绑定路由键到orderDelayQueue队列，并设置过期时间
 * 2过期时间到了修改路由键通过路由键绑定到orderReleaseOrderQueue队列
 * 3消费者消费orderReleaseOrderQueue队列的消息
 */
@Configuration
public class MyMQConfig {

    /**
     * 死信队列
     * @return
     * @Bean :容器中的Binding,Queue,Exchange 都会自动创建（Rabbimq没有的情况）
     * RabbitMQ 只要有，即使属性发生变化也不会覆盖
     */
    @Bean
    public Queue orderDelayQueue(){
        /**
         * x-dead-letter-exchange: order-event-exchange
         * x-dead-letter-routing-key:order.release.order
         * x-message-ttl: 60000
         * arguments:到期时间到了之后成为死信，将信息重新发回order-event-exchange交换机，
         * 同时改变路由键为order.release.order，就可以绑定到orderReleaseOrderQueue队列，让消费者监听，从而解锁库存
         *
         */
        Map<String,Object> arguments=new HashMap<>();
        arguments.put("x-dead-letter-exchange","order-event-exchange");
        arguments.put("x-dead-letter-routing-key","order.release.order");
        arguments.put("x-message-ttl",10000);
        Queue queue = new Queue("order.delay.queue", true, false, false,arguments);
        return queue;
    }

    /**
     * 消费者监听队列
     * @return
     */
    @Bean
    public Queue orderReleaseOrderQueue(){
        Queue queue = new Queue("order.release.order.queue", true, false, false);
        return queue;
    }

    /**
     * 交换机
     * @return
     */
    @Bean
    public Exchange orderEventExchange(){
        return new TopicExchange("order-event-exchange",true,false);
    }

    /**
     * 绑定Binding orderDelayQueue队列
     * @return 这里指定队列绑定的路由键
     */
    @Bean
    public Binding orderCreateOrederBinding(){
        /**
         * String destination, Binding.DestinationType destinationType,
         * String exchange, String routingKey, Map<String, Object> arguments
         */
        return new Binding("order.delay.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.create.order",
                null);
    }

    /**
     * 绑定Binding orderReleaseOrderQueue队列
     * @return
     */
    @Bean
    public Binding orderReleaseOrederBinding(){
        return new Binding("order.release.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.order",
                null);
    }
    /**
     * 订单释放，直接和库存释放进行绑定
     */
    @Bean
    public Binding orderReleaseOtherBinding(){
        return new Binding("stock.release.stock.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.other.#",
                null);
    }
}
